package com.xuecheng.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.basic.domain.Category;
import com.xuecheng.basic.mapper.CategoryMapper;
import com.xuecheng.basic.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.commons.model.vo.CategoryVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程分类 服务实现类
 * </p>
 *
 * @author HuangYuwu
 * @since 2022-09-11
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    /**
     *  查询所有课程分类
     * @return
     */
    @Override
    public ResponseResult findAll() {
        // 1.查询一级节点分类（parentId=0）
        LambdaQueryWrapper<Category>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentid,"0");
        Category category = this.getOne(queryWrapper);
        // 一级节点转vo
        CategoryVo categoryVo = BeanHelper.copyProperties(category, CategoryVo.class);
        // 2.查询二级节点分类（parendId=一级id）
        LambdaQueryWrapper<Category>queryWrapper2=new LambdaQueryWrapper<>();
        queryWrapper2.eq(Category::getParentid,categoryVo.getId());
        List<Category> list = this.list(queryWrapper2);
        // 二级节点转vo
        List<CategoryVo> secondVoList = BeanHelper.copyWithCollection(list, CategoryVo.class);
        // 一级节点添加二级节点
        categoryVo.setCategoryTreeNodes(secondVoList);
        // 3.查询三级节点分类（parendId=二级id） 已优化
        List<String>twoIdList=new ArrayList<>();
        for (CategoryVo vo : secondVoList) {
            twoIdList.add(vo.getId());
        }
        LambdaQueryWrapper<Category>queryWrapper3=new LambdaQueryWrapper<>();
        queryWrapper3.in(Category::getParentid,twoIdList);
        List<Category> threeList = this.list(queryWrapper3);
        List<CategoryVo> threeVoList = BeanHelper.copyWithCollection(threeList, CategoryVo.class);
        for (CategoryVo vo : secondVoList) {
            List<CategoryVo> collect = threeVoList.stream()
                    .filter(threeVo -> threeVo.getParentid()
                            .equals(vo.getId())).collect(Collectors.toList());
            vo.setCategoryTreeNodes(collect);
        }
      /*  for (CategoryVo vo : secondVoList) {
            LambdaQueryWrapper<Category>queryWrapper3=new LambdaQueryWrapper<>();
            queryWrapper3.eq(Category::getParentid,vo.getId());
            List<Category> threeList = this.list(queryWrapper3);
            // 三级接点转vo
            List<CategoryVo> threeVoList = BeanHelper.copyWithCollection(threeList, CategoryVo.class);
            // 二级节点添加三级节点
            vo.setCategoryTreeNodes(threeVoList);
        }*/
        // 4.返回一级节点vo
        return ResponseResult.okResult(categoryVo);
    }
}
